﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Globalization;
using Ext.Net;
using InformationSystem.BLL.Core.Service;
using InformationSystem.BLL.Core.Model;
using System.IO;

namespace InformationSystem.WebSite.Module.DocumentMgr
{
    public partial class DocumentEdit : BasePage
    {
        public ISharedDocumentService SharedDocumentService;
        LoginUser user = null;

        protected void Page_Load(object sender, EventArgs e)
        {
            user = (LoginUser)Session["LoginUser"];
            if (!X.IsAjaxRequest && !IsPostBack)
            {
                SharedDocumentStore.DataSource = SharedDocumentService.GetDocuments(user.CollegeID, user.GradeID);
                SharedDocumentStore.DataBind();
            }


        }

        protected void BeforeRecordDeleted(object sender, BeforeRecordDeletedEventArgs e)
        {
            FileInformation fin = e.Object<FileInformation>();
            if (user.LoginUserType == UserType.Admin)
            {
                string path = Path.Combine(Server.MapPath("~"), "SharedFileFold", fin.CollegeId.ToString(), fin.GradeId.ToString(), fin.SaveFileName);
                if (File.Exists(path))
                {
                    File.Delete(path);
                }
                bool result = this.SharedDocumentService.DeleteDocument(fin.FileID);
                X.Msg.Show(new MessageBoxConfig
                   {
                       Buttons = MessageBox.Button.OK,
                       Icon = MessageBox.Icon.INFO,
                       Title = "信息",
                       Message = "删除成功！"
                   });
            }
            if (user.LoginUserType == UserType.Assistant)
            {
                FileInformation realfin = SharedDocumentService.GetDocumentDetailByID(fin.FileID);
                if (realfin.CreateUserID == user.UserID)
                {
                    string path = Path.Combine(Server.MapPath("~"), "SharedFileFold", fin.CollegeId.ToString(), fin.GradeId.ToString(), fin.SaveFileName);
                    if (File.Exists(path))
                    {
                        File.Delete(path);
                    }
                    bool result = this.SharedDocumentService.DeleteDocument(fin.FileID, user.UserID);
                    if (result)
                    {
                        X.Msg.Show(new MessageBoxConfig
                        {
                            Buttons = MessageBox.Button.OK,
                            Icon = MessageBox.Icon.INFO,
                            Title = "信息",
                            Message = "删除成功！"
                        });
                    }
                }
                else
                {
                    X.Msg.Show(new MessageBoxConfig
                    {
                        Buttons = MessageBox.Button.OK,
                        Icon = MessageBox.Icon.INFO,
                        Title = "信息",
                        Message = "此文件可能不存在或你没有权限删除此文件！"
                    });
                }
            }
        }
        protected void AfterStoreChanged(object sender, AfterStoreChangedEventArgs e)
        {
            SharedDocumentStore.DataSource = SharedDocumentService.GetDocuments(user.CollegeID, user.GradeID);
            SharedDocumentStore.DataBind();
            this.SharedDocumentStore.RefreshAfterSaving = RefreshAfterSavingMode.Always;
        }



        protected void btnDownload_Click(object sender, DirectEventArgs e)
        {
            int id = 0;
            if (!int.TryParse(e.ExtraParams["hdnfileid"], out id))
            {
                X.Msg.Show(new MessageBoxConfig
                {
                    Buttons = MessageBox.Button.OK,
                    Icon = MessageBox.Icon.INFO,
                    Title = "信息",
                    Message = "参数有错，下载失败！"
                });
                return;
            }
            FileInformation file = SharedDocumentService.GetDocumentDetailByID(id);
            if (file == null)
            {
                X.Msg.Show(new MessageBoxConfig
                {
                    Buttons = MessageBox.Button.OK,
                    Icon = MessageBox.Icon.INFO,
                    Title = "信息",
                    Message = "此文件不存在，下载失败！"
                });
                return;
            }
            string path = Path.Combine(Server.MapPath("~"), "SharedFileFold", file.CollegeId.ToString(), file.GradeId.ToString(), file.SaveFileName);
            if (File.Exists(path))
            {
                Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
                String httpHeader = "attachment;filename=" + HttpUtility.UrlEncode(file.UploadFileName);
                this.Response.Clear();
                this.Response.AddHeader("Content-Disposition", httpHeader);                
                this.Response.WriteFile(path);
                this.Response.End();
            }
            else
            {
                X.Msg.Show(new MessageBoxConfig
                {
                    Buttons = MessageBox.Button.OK,
                    Icon = MessageBox.Icon.INFO,
                    Title = "信息",
                    Message = "此文件不存在，下载失败！"
                });
                return;
            }
        }

        protected void btnRefresh_Click(object sender, EventArgs e)
        {
            SharedDocumentStore.DataSource = SharedDocumentService.GetDocuments(user.CollegeID, user.GradeID);
            SharedDocumentStore.DataBind();
        }

        protected override bool CommitPermissionCheck()
        {
            throw new NotImplementedException();
        }
    }
}
